﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PSE.Deployment.Util
{
    public static class File
    {
        public static Int64 CalculateFolderSize(string folderPath)
        {
            if (Directory.Exists(folderPath))
            {
                //recuperar os arquivos e calcular o tamanho
                string[] files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);

                return CalculateFilesSize(files);
            }
            else
            {
                return 0;
            }

        }

        public static Int64 CalculateFileSize(string file)
        {

            if (System.IO.File.Exists(file))
            {
                System.IO.FileInfo info = new System.IO.FileInfo(file);
                return info.Length;
            }
            else
            {
                return 0;
            }
        }

        public static Int64 CalculateFilesSize(string[] files)
        {
            if (files == null)
                return 0;

            Int64 size = 0;
            foreach (string file in files)
            {
                size += CalculateFileSize(file);
            }
            return size;
        }

        public static Int64 CalculateFilesSize(string[] files, string path)
        {
            if (files == null)
                return 0;

            Int64 size = 0;
            foreach (string file in files)
            {
                size += CalculateFileSize(path + file);
            }
            return size;
        }

        public static int CountFiles(string folderPath)
        {
            if (Directory.Exists(folderPath))
            {
                return CountFiles(folderPath, "*.*");
            }
            else
            {
                return 0;
            }
        }

        public static int CountFiles(string folderPath, string searchPattern)
        {
            if (Directory.Exists(folderPath))
            {
                //recuperar os arquivos e contar
                string[] files = Directory.GetFiles(folderPath, searchPattern, SearchOption.AllDirectories);

                return files.Length;
            }
            else
            {
                return 0;
            }
        }

        public static IEnumerable<string> GetFiles(string path, List<string> files)
        {
            List<string> foundFiles = new List<string>();

            if (Directory.Exists(path))
            {
                foreach (string file in files)
                {
                    foundFiles.AddRange(Directory.GetFiles(path, file, SearchOption.AllDirectories));
                }
            }

            return foundFiles;
        }

        public static void DeleteEmptyDirectories(string basePath, string directory)
        {
            if (basePath.EndsWith("\\"))
                basePath = basePath.TrimEnd('\\');

            if (directory.EndsWith("\\"))
                directory = directory.TrimEnd('\\');

            if (Directory.GetFiles(directory).Length == 0 &&
                Directory.GetDirectories(directory).Length == 0)
            {
                string parent = Directory.GetParent(directory).FullName;
                Directory.Delete(directory);

                if (directory != basePath)
                    DeleteEmptyDirectories(basePath, parent);
            }
        }

        /// <summary>
        /// Remover o primeiro nivel de diretorio do path informado
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string RemoveRootLevel(string path)
        {
            if (string.IsNullOrEmpty(path))
                return string.Empty;
            else
                return path.Remove(0, 1 + path.IndexOf(Path.DirectorySeparatorChar));
        }
    }
}
